package com.punch.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.punch.common.enums.user.role.UserRoleEnum;
import com.punch.common.exception.BusinessException;
import com.punch.common.exception.CommonExceptionEnum;
import com.punch.common.page.PageInfo;
import com.punch.common.query.user.supplier.SupplierQueryInfo;
import com.punch.common.service.impl.SuperServiceImpl;
import com.punch.common.tools.BasePage;
import com.punch.common.vo.user.supplier.SimpleSupplierVO;
import com.punch.common.vo.user.supplier.SupplierVO;
import com.punch.user.exception.UserExceptionEnum;
import com.punch.user.mapper.SysSupplierMapper;
import com.punch.user.mapper.SysUserOrganMapper;
import com.punch.user.mapper.SysUserSchoolMapper;
import com.punch.user.model.SysSupplier;
import com.punch.user.model.SysUserOrgan;
import com.punch.user.model.SysUserSchool;
import com.punch.user.service.SupplierService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SupplierServiceImpl extends SuperServiceImpl<SysSupplierMapper, SysSupplier> implements SupplierService {

    @Resource
    private SysUserSchoolMapper sysUserSchoolMapper;

    @Resource
    private SysUserOrganMapper sysUserOrganMapper;

    /**
     * 供应商列表
     *
     * @param queryInfo 查询条件
     * @return SupplierVO
     */
    @Override
    public PageInfo<SupplierVO> queryPage(SupplierQueryInfo queryInfo) {
        return BasePage.queryBasePage(queryInfo, params -> baseMapper.queryCount(params),
                params -> baseMapper.queryList(params));
    }

    /**
     * 新增
     *
     * @param supplierVO 供应商信息
     * @return 供应商id
     */
    @Override
    public Long saveSupplier(SupplierVO supplierVO) {
        SysSupplier sysSupplier = new SysSupplier();
        BeanUtils.copyProperties(supplierVO, sysSupplier);
        if (!save(sysSupplier)) {
            throw new BusinessException(CommonExceptionEnum.SAVE_ERROR);
        }
        return sysSupplier.getId();
    }

    /**
     * 修改
     *
     * @param supplierVO 供应商信息
     * @return 供应商id
     */
    @Override
    public Long updateSupplier(SupplierVO supplierVO) {
        SysSupplier sysSupplier = new SysSupplier();
        BeanUtils.copyProperties(supplierVO, sysSupplier);
        sysSupplier.setId(supplierVO.getSupplierId());
        if (!updateById(sysSupplier)) {
            throw new BusinessException(CommonExceptionEnum.UPDATE_ERROR);
        }
        return sysSupplier.getId();
    }

    /**
     * 删除供应商
     *
     * @param supplierId 供应商id
     * @return 供应商id
     */
    @Override
    public Long deleteSupplier(Long supplierId) {
        if (!removeById(supplierId)) {
            throw new BusinessException(CommonExceptionEnum.DELETE_ERROR);
        }
        return supplierId;
    }

    /**
     * 用户修改学校/机构信息,供应商跟随改变
     *
     * @param userId 用户id
     * @param roleId 角色id
     */
    @Override
    public void updateSupplierWithUser(Long userId, Integer roleId) {
        SysSupplier sysSupplier = new SysSupplier();
        if (UserRoleEnum.SCHOOL.getCode() == roleId) {
            QueryWrapper<SysUserSchool> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SysUserSchool::getUserId, userId);
            SysUserSchool sysUserSchool = sysUserSchoolMapper.selectOne(queryWrapper);
            sysSupplier.setBankUserName(sysUserSchool.getBankUserName());
            sysSupplier.setBankAccount(sysUserSchool.getBankAccount());
            sysSupplier.setBankOpen(sysUserSchool.getBankOpen());
        }
        if (UserRoleEnum.ORGAN.getCode() == roleId) {
            QueryWrapper<SysUserOrgan> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SysUserOrgan::getUserId, userId);
            SysUserOrgan sysUserOrgan = sysUserOrganMapper.selectOne(queryWrapper);
            sysSupplier.setBankUserName(sysUserOrgan.getBankUserName());
            sysSupplier.setBankAccount(sysUserOrgan.getBankAccount());
            sysSupplier.setBankOpen(sysUserOrgan.getBankOpen());
        }
        UpdateWrapper<SysSupplier> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysSupplier::getUserId, userId);
        update(sysSupplier, updateWrapper);


    }

    /**
     * 供应商列表-简单信息
     *
     * @return List<SimpleSupplierVO>
     */
    @Override
    public List<SimpleSupplierVO> listSupplierSimple() {
        List<SimpleSupplierVO> retList = new ArrayList<>();
        QueryWrapper<SysSupplier> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(SysSupplier::getUpdateTime);
        list(queryWrapper).forEach(sysSupplier -> {
            SimpleSupplierVO simpleSupplierVO = new SimpleSupplierVO();
            simpleSupplierVO.setSupplierId(sysSupplier.getId());
            simpleSupplierVO.setSupplierName(sysSupplier.getSupplierName());
            retList.add(simpleSupplierVO);
        });
        return retList;
    }

    /**
     * 获取供应商
     *
     * @param supplierId
     */
    @Override
    public SupplierVO getSupplier(Long supplierId) {
        SysSupplier sysSupplierEX = getById(supplierId);
        if (sysSupplierEX == null) {
            throw new BusinessException(UserExceptionEnum.SUPPLIER_NOT_EXIST);
        }
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(sysSupplierEX, supplierVO);
        supplierVO.setSupplierId(supplierId);
        return supplierVO;
    }
}
